

package cn.liyao.web.controller;

import cn.liyao.tool.annotation.RequireToken;
import cn.liyao.tool.api.R;
import cn.liyao.web.holder.CurrentUserIdHolder;
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.MinioClient;
import io.minio.errors.*;
import io.minio.http.Method;
import org.springframework.beans.factory.annotation.Autowired;;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.URL;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@RestController
public class MinioController {


    private final MinioClient minioClient;

    @Autowired
    public MinioController(MinioClient minioClient) {
        this.minioClient = minioClient;
    }

    @GetMapping("/getMinioUrl")
    @RequireToken()
    public R friendList(String filename) throws Exception {
       String userId =  CurrentUserIdHolder.getCurrentUser();

        String url = minioClient.getPresignedObjectUrl(
                GetPresignedObjectUrlArgs.builder()
                        .method(Method.PUT)
                        .bucket("im-server")
                        .object( userId +"/"+filename)
                        .expiry(60 * 60) // 有效期1小时
                        .build()
        );

        return R.data(url);
    }

}